﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Hope.Gallery.Models;
using System.Threading.Tasks;
using Hope.Gallery.Core;

namespace Hope.Gallery.Controllers
{
    public class AdminController : Controller
    {
        ViewModels.AdminViewModel adminViewModel = null;

        public AdminController()
        {
            adminViewModel = new ViewModels.AdminViewModel();
        }

        public ActionResult Index()
        {
            return View();
        }

        #region Create new album

        public ActionResult CreateNew()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateNew(Album album)
        {
            if (ModelState.IsValid)
            {
                adminViewModel.AddAlbum(album);
                return RedirectToAction("Albums");
            }

            return View(album);
        }

        public ActionResult Albums()
        {
            adminViewModel.LoadAlbums();
            return View(adminViewModel);
        }

        #endregion

        #region Upload

        public ActionResult Upload()
        {
            return View();
        }

        [HttpPost]
        public ActionResult MakeUpload(int? chunk, string name, string album)
        {
            var result = adminViewModel.Upload(chunk, name, album);
            return Content(result, "text/plain");
        }

        #endregion

        #region Thumbnail

        [HttpPost]
        public void Thumbnail(string uploadedFilename, int albumName)
        {

            adminViewModel.AddPictureInAlbum(albumName, uploadedFilename);

            string uploadedFilePath = DirectoryHelper.GetPhysicalUploadedFilePath(uploadedFilename, albumName.ToString());
            string thumbPath = DirectoryHelper.GetPhysicalThumbPathForAlbum(albumName.ToString(), uploadedFilename);

            int width = ConfigKeys.ThumbWidth;
            int height = ConfigKeys.ThumbHeight;

            Task.Factory.StartNew(() =>
            {
                PictureResize.ResizePicture(uploadedFilePath, thumbPath, width, height);
            });
        }
        #endregion

        public ActionResult DeleteAlbum(string album, int albumId)
        {

            return View();
        }
    }
}
